<?php

// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------


namespace app\common\model;


use think\Model;
use think\model\concern\SoftDelete;

/**
 * 有关时间的模型
 * Class TimeModel
 * @package app\common\model
 */
class TimeModel extends Model
{

    /**
     * 自动时间戳类型
     * @var string
     */
    protected $autoWriteTimestamp = true;

    /**
     * 添加时间
     * @var string
     */
    protected $createTime = 'create_time';

    /**
     * 更新时间
     * @var string
     */
    protected $updateTime = 'update_time';

    /**
     * 软删除
     */
    use SoftDelete;

    protected $deleteTime = false;

    /**
     * 公用获取所有数据转换为数组
     * @param array $where
     * @param string $field
     * @param string $order
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getModelByListToArray(array $where = [], string $field = '*', string $order = "id desc"): array
    {
        return $this->field($field)
            ->where($where)
            ->order($order)
            ->select()
            ->toArray();
    }

    /**
     *
     * @param array $where
     * @param string $field
     * @param string $order
     * @param int $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getModelByListTolimitArray(array $where = [], string $field = '*', int $limit, string $order = "id desc"): array
    {
        return $this->field($field)
            ->where($where)
            ->order($order)
            ->limit($limit)
            ->select()
            ->toArray();
    }


    /**
     * 公用查询指定信息
     * @param array $where
     * @param string $field
     * @return array|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getModelByInfo(array $where=[],string $field='*',string $order='id desc'){
        return $this->field($field)
            ->where($where)
            ->order($order)
            ->find();
    }

    public function getModelByCount(array $where){
        return $this->where($where)
            ->count();
    }

    public function getModelByGroup(array $where,string $field='*',string $group ='id'): array
    {
        return $this->field($field)
            ->where($where)
            ->group($group)
            ->select()
            ->toArray();
    }

    public function deleteByIds(array $where){
        return $this->where($where)->delete();
    }


}